package cn.kais.drawer.unit;

import cn.kais.drawer.UnitTools;
import cn.kais.drawer.UnitUtil;
import cn.kais.drawer.bean.BeanTime;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.text.AttributedString;
import java.util.Date;

/**
 * 时间
 */
public class UnitTime {

    public void test() {
        BeanTime bean = new BeanTime(null, null);
        BufferedImage ima = drawer(bean);
        UnitTools.doTestWrite(ima, "TestTime");

        DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    public BufferedImage drawer(BeanTime bean) {
        BufferedImage ima = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB);
        Graphics2D sd = ima.createGraphics();
        sd.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        Font font = new Font("微软雅黑", Font.BOLD | Font.ITALIC, 44);
        Color color = Color.BLACK;
        FontMetrics metrics = sd.getFontMetrics(font);

        Date date = UnitUtil.doStr2Date(bean.text);
        String time = UnitUtil.doDate2Str(date, bean.dateFormat);
        time = doReplaceSeparator(time, bean.dateSeparator);
        AttributedString as = new AttributedString(time);
        as.addAttribute(TextAttribute.FONT, font);
        as.addAttribute(TextAttribute.FOREGROUND, color);
        sd.drawString(as.getIterator(), 0, 44);

        sd.dispose();
        return ima;
    }


    /**
     * 替换分隔符
     */
    public String doReplaceSeparator(String time, String separator) {
        return time.replaceAll("-", separator);
    }

}
